﻿
Imports System.DateTime
Imports osi.root.connector
Imports osi.root.lock
Imports osi.root.constants
Imports osi.root.constants.counter
Imports System.Threading

Namespace counter
    Partial Friend Class counter_record
        Private Function hit_sample() As Boolean
            Return sample_rate = 1 OrElse rnd(0, 1) < sample_rate
        End Function

        Public Function increase(ByVal dc As lazier(Of Int64)) As Boolean
            If hit_sample() Then
                Dim startticks As Int64 = 0
                If envs.counter_selfhealth Then
                    startticks = Now().Ticks()
                End If
                Dim c As Int64 = 0
                c = +dc
                Dim index As Int64 = 0
                index = Interlocked.Increment(calltimes) - 1
                If count_selected() OrElse
                   average_selected() OrElse
                   rate_selected() Then
                    Interlocked.Add(count, c)
                End If
                If last_average_selected() OrElse
                   last_rate_selected() Then
                    last_averages(index Mod last_averages.Length()) = c
                End If
                If last_rate_selected() Then
                    If Not envs.counter_selfhealth Then
                        startticks = Now().Ticks()
                    End If
                    last_times_ticks(index Mod last_times_ticks.Length()) = startticks
                End If
                If envs.counter_selfhealth Then
                    selfhealth.record_increase_latency(startticks)
                End If
            End If

            Return True
        End Function
    End Class
End Namespace
